package com.guojiang.guoaicodemother.service;

import cn.hutool.core.util.StrUtil;
import com.guojiang.guoaicodemother.exception.ErrorCode;
import com.guojiang.guoaicodemother.exception.ThrowUtils;
import com.guojiang.guoaicodemother.model.dto.chatHistoryDto.ChatHistoryQueryRequest;
import com.guojiang.guoaicodemother.model.entity.User;
import com.guojiang.guoaicodemother.model.enums.ChatHistoryMessageTypeEnum;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.service.IService;
import com.guojiang.guoaicodemother.model.entity.ChatHistory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;

import java.time.LocalDateTime;

/**
 * 对话历史 服务层。
 *
 * @author <a href="https://gitee.com/jam-altman">程序员果酱</a>
 */
public interface ChatHistoryService extends IService<ChatHistory> {


    /**
     * 添加对话历史记录
     *
     * @param appId
     * @param message
     * @param messageType
     * @param userId
     * @return
     */
    boolean addChatMessage(Long appId, String message, String messageType, Long userId);


    /**
     * 根据应用 ID 删除对话历史记录
     *
     * @param appId
     * @return
     */
    boolean deleteChatMessageByAppId(Long appId);

    /**
     * 游标查询对话历史记录
     *
     * @param appId
     * @param pageSize
     * @param lastCreateTime
     * @param loginUser
     * @return
     */
    Page<ChatHistory> listAppChatHistoryByPage(Long appId, int pageSize, LocalDateTime lastCreateTime, User loginUser);

    /**
     * 初始化 AI 服务实例时，将数据库中的对话历史记录加载到对话记忆中
     *
     * @param appId
     * @param chatMemory
     * @return
     */
    int loadChatHistoryToMemory(long appId, MessageWindowChatMemory chatMemory);

    /**
     * 构造查询条件
     *
     * @param chatHistoryQueryRequest
     * @return
     */
    QueryWrapper getQueryWrapper(ChatHistoryQueryRequest chatHistoryQueryRequest);
}
